//
//  MZExtensionString.swift
//  MZTool
//
//  Created by gby on 2022/2/14.
//

import UIKit


extension MZKit where Base == String {
    
    //MARK: 判断纯数字
    public func isPureInt() -> Bool {
        let scan: Scanner = Scanner(string: base)
        var val:Int = 0
        return scan.scanInt(&val) && scan.isAtEnd
    }
    
    //MARK: 宽度
    public func boundingSize(font: UIFont, maxW: CGFloat) -> CGSize {
        let text = base as NSString
        return text.boundingRect(with: CGSize(width: maxW, height: CGFloat.greatestFiniteMagnitude), options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font:font], context: nil).size
    }
    
    //MARK: base64Decoding
    public func base64Decoding() -> String {
        let decodeData = NSData.init(base64Encoded: base, options: NSData.Base64DecodingOptions.init(rawValue: 0))
        if decodeData == nil || decodeData?.length == 0 {
            return "";
        }
        let decodeString = NSString(data: decodeData! as Data, encoding: String.Encoding.utf8.rawValue)
        return decodeString! as String
    }
    
    
    
    public static func timeStr(seconds: Int64) -> String{
        if seconds <= 0 {
            return ""
        }
        let totalHours  :Int = Int(seconds / (60*60))
        let days        :Int = totalHours / 24
        let hours       :Int = totalHours % 24
        let minutes     :Int = Int(seconds % 3600 / 60)
        let seconds     :Int = Int(seconds) % 60
        
        var timeStr = ""
        if days > 0 {
            timeStr = timeStr + String(format:"%02ld",totalHours)
        }
        if hours > 0 {
            timeStr = timeStr + String(format:"%02ld:",hours)
        }
        if minutes > 0 {
            timeStr = timeStr + String(format:"%02ld:",minutes)
        }
        var more = ""
        if timeStr.count == 0{
            more = "s"
        }
        if seconds < 10, timeStr.count == 0{
            timeStr = timeStr + String(format:"%ld%@",seconds,more)
        }else{
            timeStr = timeStr + String(format:"%02ld%@",seconds,more)
        }
        return timeStr
    }
}
